package com.study.base;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @description: ExecutorsTest  线程池工具类使用
 * @date: 2020/11/12 12:04
 * @author: hcm
 * @version: 1.0
 */
public class ExecutorsTest {

    public static void main(String[] args) {
        //固定大小的线程池,不管executor执行几次都只以两个线程执行代码
        //ExecutorService executorService = Executors.newFixedThreadPool(2);

        //无论如何都是单个线程执行
        //ExecutorService executorService = Executors.newSingleThreadExecutor();

        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2);

        MyThread myThread1 = new MyThread();
        MyThread myThread2 = new MyThread();

        MyThread myThread3 = new MyThread();

        MyThread myThread4 = new MyThread();

        executorService.execute(myThread1);

        executorService.execute(myThread2);
        executorService.execute(myThread3);
        executorService.execute(myThread4);

        //开启延时
       executorService.schedule(myThread3,10, TimeUnit.MILLISECONDS);
       executorService.schedule(myThread4,10,TimeUnit.MILLISECONDS);
        executorService.shutdown();
    }
}
class MyThread extends Thread{
    @Override
    public void run() {
        super.run();
        System.out.println(Thread.currentThread().getName() + ": 正在执行~~~~");
    }
}